package dao

import (
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
	"time"
	"vpn-management-server/config"
)

var RdsClient RedisClient

type RedisClient struct {
	Client *redis.Client
}

func (r *RedisClient) Init(c config.RedisConfig) {
	rdb := redis.NewClient(&redis.Options{
		Addr: fmt.Sprintf("%s:%s", c.IP, c.Port),
	})
	r.Client = rdb
}

func (r *RedisClient) SetString(ctx context.Context, key string, value string, expirationTime time.Duration) error {
	_, err := r.Client.Set(ctx, key, value, expirationTime).Result()
	return err
}

func (r *RedisClient) GetString(ctx context.Context, key string) (string, error) {
	value, err := r.Client.Get(ctx, key).Result()
	return value, err
}

func (r *RedisClient) Exists(ctx context.Context, key string) (bool, error) {
	val, err := r.Client.Exists(ctx, key).Result()
	if err != nil {
		return false, err
	}
	return val > 0, nil
}
